package practice;

import java.io.*;

public class Translate {
    public static void main(String[] args) throws IOException {
        File sourceFile = new File("D:\\安装文件\\红楼梦.txt");
        File encodeFile=new File("D:\\安装文件\\加密红楼梦.txt");
        File decodeFile=new File("D:\\安装文件\\解密红楼梦.txt");


        long start = System.currentTimeMillis();
        encode(sourceFile,encodeFile);
        long end = System.currentTimeMillis();

        long start2 = System.currentTimeMillis();
        decode(encodeFile,decodeFile);
        long end2 = System.currentTimeMillis();

        long start3 = System.currentTimeMillis();
        bufferedEncode(sourceFile,encodeFile);
        long end3 = System.currentTimeMillis();

        long start4 = System.currentTimeMillis();
        bufferedDecode(encodeFile,decodeFile);
        long end4 = System.currentTimeMillis();

        System.out.println("普通字符流加密耗时：" + (end - start) + " ms");
        System.out.println("普通字符流解密耗时：" + (end2 - start2) + " ms");
        System.out.println("缓冲字符流加密耗时：" + (end3 - start3) + " ms");
        System.out.println("缓冲字符流解密耗时：" + (end4 - start4) + " ms");

    }

    // 普通字节流
    public static void encode(File sourceFile,File distFile) throws IOException {
        FileReader is = new FileReader(sourceFile);
        FileWriter os=new FileWriter(distFile);
        char temp[]=new char[1000];
        int n=-1;
        while((n=is.read(temp))!=-1){
            for(int i=0;i<n;i++){
                temp[i]=(char) (temp[i]+100);
            }
            os.write(temp);
            os.flush();

        }

    }
    public static void decode(File sourceFile,File distFile) throws IOException{
        FileReader is = new FileReader(sourceFile);
        FileWriter os=new FileWriter(distFile);
        char temp[]=new char[1000];
        int n=-1;
        while((n=is.read(temp))!=-1){
            for(int i=0;i<n;i++){
                temp[i]=(char) (temp[i]-100);
            }
            os.write(temp);
            os.flush();

        }
    }

    // 缓冲字节流
    public static void bufferedEncode(File sourceFile,File distFile) throws IOException {
        FileReader is = new FileReader(sourceFile);
        FileWriter os = new FileWriter(distFile);
        BufferedReader bis = new BufferedReader(is);
        BufferedWriter bos = new BufferedWriter(os);
        char temp[] = new char[1000];
        int n = -1;
        while ((n = bis.read(temp)) != -1) {
            for (int i = 0; i < n; i++) {
                temp[i] = (char) (temp[i] + 100);
            }
            bos.write(temp);
            bos.flush();

        }
    }
    public static void bufferedDecode(File sourceFile,File distFile) throws IOException{
        FileReader is = new FileReader(sourceFile);
        FileWriter os=new FileWriter(distFile);
        BufferedReader bis=new BufferedReader(is);
        BufferedWriter bos=new BufferedWriter(os);
        char temp[]=new char[1000];
        int n=-1;
        while((n=bis.read(temp))!=-1){
            for(int i=0;i<n;i++){
                temp[i]=(char) (temp[i]-100);
            }
            bos.write(temp);
            bos.flush();

        }
    }
}
